//
//  SettingViewController.m
//  ShangPiBao
//
//  Created by chirui on 2017/10/25.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "SettingViewController.h"
#import "LogingViewController.h"

@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    UITableView * _tableView;
    UILabel * classLabel;
    UILabel * addressLabel;
}

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    TitleView(@"设置");
//    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_nav_return" target:self action:@selector(back)];
    
    [self creatTableView];
}

-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 3;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 200;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSArray * array = @[@"消息接收",@"行业",@"地区"];
    
    static  NSString * str = @"cellid";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str];
    
    if (cell==nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }else {
        while ([cell.contentView.subviews lastObject]!=nil) {
            
            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    
    if (indexPath.row!=0) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        if (indexPath.row==1) {
            
            classLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH -200, 15, 170, 20)];
            classLabel.textAlignment = NSTextAlignmentRight;
            classLabel.font = [UIFont systemFontOfSize:14];
            classLabel.textColor = [UIColor grayColor];
            [cell.contentView addSubview:classLabel];
        }
        
        
        if (indexPath.row==2) {
            
            addressLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH -200, 15, 170, 20)];
            addressLabel.textAlignment = NSTextAlignmentRight;
            addressLabel.font = [UIFont systemFontOfSize:14];
            addressLabel.textColor = [UIColor grayColor];
            [cell.contentView addSubview:addressLabel];
        }
        
    }else {
        
        UISwitch *mySwitch = [[UISwitch alloc]initWithFrame:CGRectMake(WIDTH - 60, 10, 79, 27)];
        //开关状态  YES：为开  NO：为关
        mySwitch.on = YES;
        //响应方法
        [mySwitch addTarget:self action:@selector(switchValueChange:) forControlEvents:UIControlEventValueChanged];
        [cell.contentView addSubview:mySwitch];
    
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = array[indexPath.row];
    cell.textLabel.font =[UIFont systemFontOfSize:15];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row==1) {
        
         NSArray * titleArray = @[@"全部",@"五金机电",@"建筑材料",@"酒店用品",@"汽配汽用",@"文体办公",@"农林牧渔 ",@"服装鞋包",@"物流货运"];
      
        zySheetPickerView * pickerView = [zySheetPickerView ZYSheetStringPickerWithTitle:titleArray andHeadTitle:@"行业" Andcall:^(zySheetPickerView *pickerView, NSString *choiceString) {
            classLabel.text = choiceString;
            [pickerView dismissPicker];
        }];
        [pickerView show];
    }
    if (indexPath.row==2) {
        NSArray * bankArray = @[@"全部",@"北京市",@"天津市",@"河北省",@"山西省",@"内蒙古区",@"辽宁省",@"吉林省",@"黑龙江省",@"上海市",@"江苏省",@"浙江省",@"安徽省",@"福建省",@"江西省",@"山东省",@"河南省",@"湖北省",@"湖南省",@"广东省",@"广西区",@"海南省",@"重庆市",@"四川省",@"贵州省",@"云南省",@"西藏区",@"陕西省",@"甘肃省",@"青海省",@"宁夏区",@"新疆区",@"台湾省",@"香港特区",@"澳门特区"];
        zySheetPickerView * pickerView = [zySheetPickerView ZYSheetStringPickerWithTitle:bankArray andHeadTitle:@"地区" Andcall:^(zySheetPickerView *pickerView, NSString *choiceString) {
            addressLabel.text = choiceString;
            [pickerView dismissPicker];
        }];
        [pickerView show];
        
    }

}

-(UIView * )tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    UIView * footerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    footerView.backgroundColor = [UIColor clearColor];
    
    UIButton * sumBtn =[UIButton buttonWithType:UIButtonTypeSystem];
    [sumBtn setTitle:@"退出当前账号" forState:UIControlStateNormal];
    sumBtn.backgroundColor =[UIColor blackColor];
    [sumBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    sumBtn.frame = CGRectMake(60,150 , WIDTH-120, 45);
    sumBtn.layer.cornerRadius= 22.5f;
    sumBtn.layer.masksToBounds = YES;
    [sumBtn addTarget:self action:@selector(sumBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [footerView addSubview:sumBtn];

    return footerView;
}

-(void)sumBtnClick{
    
    USER_DEFAULTSet(@"", @"userData");
    USER_DEFAULTSet(@"", @"enter");
    USER_DEFAULTSet(@"", @"uid");
    USER_DEFAULTSet(@"", @"classId");
    LogingViewController * login =[[LogingViewController alloc]init];
    [self.navigationController pushViewController:login animated:YES];
    
    
    
}

- (void)switchValueChange:(UISwitch *)st{
   
    if (st.on) {
        NSLog(@"kai");
       
    }else{
        NSLog(@"guan");
      
    }
}



@end
